package mns.modules.output.sms;

/**
 * 
 */
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.util.Date;

/**
 * 
 * @author Dan Moore
 *
 */
public class EMail {

	private static String MAIL_SERVER = "localhost";
	
	/**
	 * 
	 * @param to
	 * @param from
	 * @param subj
	 * @param msg
	 * @return
	 */
	public static boolean sendMail(String to, String from, String subj, String msg) 
									throws NoSuchProviderException, MessagingException {
		Properties prop = new Properties();
		Session ses = null;
		Transport trans = null;
		MimeMessage eMail = null;
		
		prop.setProperty("mail.transport.protocol", "smtp");
		prop.setProperty("mail.host", MAIL_SERVER);
		//prop.setProperty("mail.user", USERNAME);
		//prop.setProperty("mail.password", PASSWORD);
		prop.setProperty("mail.from", from);

		ses = Session.getInstance(prop,null);
		trans = ses.getTransport();
		
		eMail = new MimeMessage(ses);
		eMail.setSubject(subj);
		eMail.setText(msg);
		eMail.setSentDate(new Date());
		eMail.setFrom(new InternetAddress(from));
		eMail.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		
		trans.connect();
		trans.sendMessage(eMail, eMail.getRecipients(Message.RecipientType.TO));


		if(trans != null) {
			trans.close();
		}

		return true;
	}
	
	/*
	public static void main(String[] args) {
		try {
			EMail.sendMail("dan.moore@gatech.edu", "dan.moore@gatech.edu", "test", "This is another test msg, from java + sendmail.");
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
		
}
